/*
 * 爱组搭，低代码组件化开发平台
 * ------------------------------------------
 * 受知识产权保护，请勿删除版权申明，开发平台不允许做非法网站，后果自负
 */
package com.aizuda.boot.modules.system.mapper;

import com.aizuda.boot.modules.system.entity.SysDepartment;
import com.aizuda.boot.modules.system.entity.dto.UserDepartmentDTO;
import com.aizuda.boot.modules.system.entity.vo.DepartmentHeadVO;
import com.aizuda.service.mapper.CrudMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 部门 Mapper 接口
 * </p>
 *
 * @author 青苗
 * @since 2021-11-07
 */
public interface SysDepartmentMapper extends CrudMapper<SysDepartment> {

    /**
     * 根据 id 递归子类集合
     */
    List<Long> selectIdsRecursive(@Param("id") Long id);

    /**
     * 查询用户的部门主管信息
     */
    DepartmentHeadVO selectDepartmentHeadByUserId(@Param("userId") Long userId);

    /**
     * 根据父ID获取子部门列表
     * @param pid 父部门ID
     * @return 子部门列表
     */
    List<SysDepartment> selectByPid(@Param("pid") Long pid);

    /**
     * 获取部门的所有子部门ID（包含自己）
     * @param departmentId 部门ID
     * @return 部门ID列表
     */
    @Select("""
        WITH RECURSIVE dept_tree AS (
            SELECT id FROM sys_department WHERE id = #{departmentId} AND deleted = 0
            UNION ALL
            SELECT d.id FROM sys_department d
            INNER JOIN dept_tree dt ON d.pid = dt.id
            WHERE d.deleted = 0
        )
        SELECT id FROM dept_tree
    """)
    List<Long> selectAllChildDepartmentIds(@Param("departmentId") Long departmentId);

    /**
     * 获取部门的完整路径
     * @param departmentId 部门ID
     * @return 部门路径列表（从顶级到当前部门）
     */
    @Select("""
        WITH RECURSIVE dept_path AS (
            SELECT id, pid, name, code, 1 as level FROM sys_department WHERE id = #{departmentId} AND deleted = 0
            UNION ALL
            SELECT d.id, d.pid, d.name, d.code, dp.level + 1 FROM sys_department d
            INNER JOIN dept_path dp ON d.id = dp.pid
            WHERE d.deleted = 0
        )
        SELECT * FROM dept_path ORDER BY level DESC
    """)
    List<SysDepartment> selectDepartmentPath(@Param("departmentId") Long departmentId);

    /**
     * 获取部门下的所有员工ID
     * @param departmentId 部门ID
     * @return 员工ID列表
     */
    @Select("""
        SELECT DISTINCT ud.user_id
        FROM sys_user_department ud
        WHERE ud.department_id = #{departmentId}
    """)
    List<Long> selectUserIdsByDepartmentId(@Param("departmentId") Long departmentId);

    /**
     * 获取员工的主部门信息
     * @param userId 用户ID
     * @return 部门信息
     */
    @Select("""
        SELECT d.*
        FROM sys_department d
        INNER JOIN sys_user_department ud ON d.id = ud.department_id
        WHERE ud.user_id = #{userId} AND d.deleted = 0 AND d.status = 1
        ORDER BY ud.is_main DESC, d.id
        LIMIT 1
    """)
    SysDepartment selectMainDepartmentByUserId(@Param("userId") Long userId);

    /**
     * 批量查询用户主部门信息
     * @param userIds 用户ID列表
     * @return 用户部门信息列表
     */
    List<UserDepartmentDTO> selectMainDepartmentsByUserIds(@Param("userIds") List<Long> userIds);


    List<SysDepartment> listAllParentDepartments(@Param("list") List<Long> departmentIds);

    /**
     * 获取部门下的所有子部门
     * @param departmentIds
     * @return
     */
    List<SysDepartment> listChildDepartments(List<Long> departmentIds);
}
